import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/util/DataUtil.dart';

/**
 * ///GridView({
    Axis scrollDirection = Axis.vertical,
    bool reverse = false,
    ScrollController controller,
    bool primary,
    ScrollPhysics physics,
    bool shrinkWrap = false,
    EdgeInsetsGeometry padding,
    @required SliverGridDelegate gridDelegate, //控制子widget layout的委托

    SliverGridDelegateWithFixedCrossAxisCount({
    @required double crossAxisCount,  //横轴子元素的数量
    double mainAxisSpacing = 0.0,   //主轴方向的间距
    double crossAxisSpacing = 0.0, //横轴方向子元素的间距
    double childAspectRatio = 1.0, //子元素在横轴长度和主轴长度的比例
    })
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    double cacheExtent,
    List<Widget> children = const <Widget>[],
    })
 */

///

class GridViewWidget2 extends StatefulWidget {
  final String ?text;

  GridViewWidget2({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return _GrideState(text??"");
  }
}

class _GrideState extends State<GridViewWidget2> {
  String ?title;

  _GrideState(String text) {
    title = text;
  }

  int positon = 0;

  List<String> _datas = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 初始化数据
    _datas = DataUtil.getNumber();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title??""),
      ),
      body: Container(
        padding: EdgeInsets.only(top: 20, left: 20, right: 20),
        child:
            //动态获取数据
            GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              childAspectRatio: 1.5,
              crossAxisSpacing: 10,
              mainAxisSpacing: 5),
          itemBuilder: (context, index) {
            return DecoratedBox(
              decoration: BoxDecoration(color: Colors.black12),
              child: Padding(
                padding: EdgeInsets.all(20),
                child: Text(
                  _datas[index],
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.deepOrange,
                  ),
                ),
              ),
            );
          },
          // itemCount: _icons.length,
          itemCount: _datas.length,
        ),
      ),
    );
  }
}
